package com.molin.edu.info.manager.controller;

import com.molin.edu.info.manager.domain.Student;
import com.molin.edu.info.manager.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

/*
* controller 与用户进行交互，不关心数据是否合理。*/
public abstract class BaseStudentController {
    StudentService studentService = new StudentService();

    public final void showStudentInfo(ArrayList<Student> students) {
        String name;
        int age;
        String id;
        String birthday;
        System.out.println("id\t姓名\t年龄\t出生日期");
        for (int i = 0; i < students.size(); i++) {
            Student st = students.get(i);
            name = st.getName();
            age = st.getAge();
            id = st.getId();
            birthday = st.getBirthday();
            System.out.println(id + "\t" + name + "\t" + age + "\t" + birthday);
        }

    }

    public final void start() {
        stuLoop:
        while (true) {

            System.out.println("============欢迎进入学生管理系统=============");
            System.out.println("1.添加学生 2.删除学生 3.修改学生 4.查询学生 5.退出");
            System.out.print("请输入： ");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();

            switch (choose) {
                case 1:
                    boolean result = addStudent();
                    if (result) {
                        System.out.println("学生信息添加成功");
                    } else {
                        System.out.println("学生信息添加失败");
                    }
                    break;
                case 2:
//                  System.out.println("删除学生信息。。");
                    if(dropStudentInfo()){
                        System.out.println("删除成功");
                    }else {
                        System.out.println("删除失败");
                    }
                    break;
                case 3:

//                    System.out.println("修改学信息。。");
                    if(modifyStudentInfo()){
                        System.out.println("修改成功");
                    }else {
                        System.out.println("修改失败");
                    }
                    break;
                case 4:
//                    System.out.println("查询学生信息。。");
                    ArrayList<Student> studentArrayList = new ArrayList<>();
                    studentArrayList = findAll();
                    showStudentInfo(studentArrayList);
                    break;
                case 5:
                    System.out.println("退出学生管理系统");
                    break stuLoop;
                default:
                    System.out.println("输入错误，请重新操作！");
            }
        }
    }

    private final boolean modifyStudentInfo() {
        boolean result=false;
        Scanner sc=new Scanner(System.in);
        String id;
        //要求学生已存在，才可以修改学生信息。
        while (true){
            System.out.println("请输入要删除学生信息的id： ");
            id=sc.nextLine();
            if(studentService.isExsit(id)){
                break;
            }else {
                System.out.println("输入的id不存在！请重新输入!");
            }
        }
        System.out.println("请输入学生年龄: ");
        int age = sc.nextInt();
        sc.nextLine();
        System.out.println("请输入学生生日: ");
        String birthday = sc.nextLine();
        System.out.println("请输入学生姓名: ");
        String name = sc.nextLine();
        return studentService.modifyStudent(id, age, name, birthday);
    }

    private final boolean dropStudentInfo() {
        boolean result=false;
        while (true){
            System.out.println("请输入要删除学生信息的id： ");
            Scanner sc=new Scanner(System.in);
            String id=sc.nextLine();
            if(studentService.isExsit(id)){
                result=studentService.dropStudentInfo(id);
                break;
            }else {
                System.out.println("输入的id不存在！请重新输入!");
            }
        }
        return result;

    }

    private ArrayList<Student> findAll() {
        return studentService.findAll();
    }

    public abstract boolean addStudent();
}
